package cn.icanci.task.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author: icanci
 * @ProjectName: Spring-Boot-Cases
 * @PackageName: cn.icanci.task.service
 * @Date: Created in 2020/7/18 12:39
 * @ClassAction: 邮件任务
 */
@Service
public class JavaMailBy163Service {

    @Autowired
    JavaMailSenderImpl mailSender;

    public void sendMail(String message) {
        // 一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 标题
        simpleMailMessage.setSubject("通知：ic你好");
        // 内容
        simpleMailMessage.setText(message);
        // 发给谁
        simpleMailMessage.setTo("icanci@foxmail.com");
        // 谁发的
        simpleMailMessage.setFrom("icanci@163.com");
        // 发送
        mailSender.send(simpleMailMessage);
    }

    public void sendMoreMail(String message) throws MessagingException {
        // 一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 标题
        helper.setSubject("通知：ic你好");
        // 发送的内容
        String text = "<p style=\"text-align: center;\">欢迎您注册：<a href=\"http://icanci.cn/\">http://icanci.cn/</a></p>\n" +
                "<p style=\"text-align: center;color: cornflowerblue;\">我们将竭诚为您服务！</p>\n" +
                "<p style=\"text-align: center;color: cornflowerblue;\">" + message + "</p>\n";
        // 设置内容，是否为html数据
        helper.setText(text, true);
        // 附件
        helper.addAttachment("213055.jpg", new File("G:\\360downloads\\213055.jpg"));
        helper.addAttachment("1000065.jpg", new File("G:\\360downloads\\1000065.jpg"));
        // 发给谁
        helper.setTo("icanci@foxmail.com");
        // 谁发的
        helper.setFrom("icanci@163.com");
        mailSender.send(mimeMessage);
    }
}